import java.io.*;
import java.util.ArrayList;

public class Utils {

    //read hmm modle file from local file , to string array
    public static ArrayList<String[]> readHmmFileToStringArray(String filePath){
        ArrayList<String[]> result = new ArrayList<>();
        try {
            String encoding="UTF-8";
            File file = new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt ;
                while((lineTxt = bufferedReader.readLine()) != null)
                {
                    lineTxt.replaceAll(" +"," ");
                    String[] res = lineTxt.split(" ");
                        if (res != null)
                        {
                            result.add(res);
                        }
                }
                read.close();
                return result;
            }else{
                System.out.println("can not find file");
                return null;
            }
        } catch (Exception e) {
            System.out.println("read file error");
            e.printStackTrace();
        }
        return result;
    }

    //read text file from dir, return its ArrayList<Double>
    public static ArrayList<Double> readTxtFileToDoubleArray(String filePath){
        ArrayList<Double> result = new ArrayList<>();
        try {
            String encoding="UTF-8";
            File file = new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt ;
                while((lineTxt = bufferedReader.readLine()) != null){
                    String[] res = lineTxt.split(" ");
                    for (String s:res) {
                        if (!s.isBlank()){
                            result.add(Double.valueOf(s));
                        }
                    }
                }
                read.close();
                return result;
            }else{
                System.out.println("can't find file");
                return null;
            }
        } catch (Exception e) {
            System.out.println("read file error");
            e.printStackTrace();
        }
        return result;
    }

    //read text file from dir, return its ArrayList<String>
    public static ArrayList<String> readTxtFileToString(String filePath){
        ArrayList<String> result = new ArrayList<>();
        try {
            String encoding="UTF-8";
            //int i = 0;
            File file = new File(filePath);
            if(file.isFile() && file.exists()){ //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt ;
                while( bufferedReader.readLine() != null){
                    //bufferedReader.skip(10);
                    lineTxt = bufferedReader.readLine();

                    result.add(lineTxt.toUpperCase());
                    }
                read.close();
                }
                return result;
            } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (FileNotFoundException e1) {
            System.out.println("can't find file ");
            e1.printStackTrace();
        } catch (IOException e1) {
            System.out.println("read file error");
            e1.printStackTrace();
        }
        return result;
    }

    //calculate log Lisr and return a List
    public static ArrayList<Double> log(ArrayList<Double> model){
        ArrayList<Double> logModel = new ArrayList<>();
        for (Double m:model) {
            m = Math.log(m) / Math.log(2);
            logModel.add(m);
        }
        return logModel;
    }

    //calculate the probability of a string using define model,return its sum
    public static double dataProbabilitySum(String dataString,ArrayList<Double> logModel){
        //ArrayList<StringBuilder> dataStringBuilder = new ArrayList<>();
        Double sum = 0.00;
        char[] floatTemp;
        floatTemp = dataString.toCharArray();
        for (int i=0;i<floatTemp.length-1;i++){
            switch (floatTemp[i]){
                case 'A':
                    switch (floatTemp[i+1]){
                        case 'A':
                           sum+=logModel.get(0);break;
                        case 'C':
                            sum+=logModel.get(1);break;
                        case 'G':
                            sum+=logModel.get(2);break;
                        case 'T':
                            sum+=logModel.get(3);break;
                         default: break;
                    }
                case 'C':
                    switch (floatTemp[i+1]){
                        case 'A':
                            sum+=logModel.get(4);break;
                        case 'C':
                            sum+=logModel.get(5);break;
                        case 'G':
                            sum+=logModel.get(6);break;
                        case 'T':
                            sum+=logModel.get(7);break;
                        default: break;
                    }
                case 'G':
                    switch (floatTemp[i+1]){
                        case 'A':
                            sum+=logModel.get(8);break;
                        case 'C':
                            sum+=logModel.get(9);break;
                        case 'G':
                            sum+=logModel.get(9);break;
                        case 'T':
                            sum+=logModel.get(11);break;
                        default: break;
                    }
                case 'T':
                    switch (floatTemp[i+1]){
                        case 'A':
                            sum+=logModel.get(12);break;
                        case 'C':
                            sum+=logModel.get(13);break;
                        case 'G':
                            sum+=logModel.get(14);break;
                        case 'T':
                            sum+=logModel.get(15);break;
                        default: break;
                    }
            }
        }
        return sum;


    }

    //read a file to a charList,input the filePath
    public static ArrayList<Character> readFileToCharacterList(String filePath){
        ArrayList<Character> charArrayList = new ArrayList<>() ;

        try {
            String encoding="UTF-8";
            File file = new File(filePath);
            if(file.isFile() && file.exists())
            { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(new FileInputStream(file),encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String stringTemp;
                char[] charArrayTemp;
                int i1 = 1;
                while((stringTemp =bufferedReader.readLine()) != null)
                {
                    if (i1 == 1){
                        i1=0;
                        continue;
                    }
                    charArrayTemp = stringTemp.toUpperCase().toCharArray();
                    for (char c:charArrayTemp)
                    {
                        charArrayList.add(c);
                    }
                }
                read.close();
            }
                return charArrayList;
            } catch (UnsupportedEncodingException e) {
            System.out.println("file encoding error");
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            System.out.println("file not found");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("file read error");
            e.printStackTrace();
        }
        return charArrayList;
    }

    //write the hiddenSequence to local file
    public static void writeToFile(char[] array){
        try{

            File file =new File("opt.txt");
            System.out.print("chuangjianchenggong");

            //if file doesnt exists, then create it
//            if(!file.exists()){
//                file.createNewFile();
//            }
            file.createNewFile();
            //true = append file
            FileWriter fileWritter = new FileWriter(file.getName(),true);
            int n = 0;
            for(char hiddenState : array)
            {
                if (n-59 >= 0){
                    n=0;
                    fileWritter.write(hiddenState);
                    fileWritter.write('\n');
                }else{
                    fileWritter.write(hiddenState);
                    n++;
                }
            }
            fileWritter.close();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

}
